package com.hzncc.flowable_diboot.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.diboot.core.util.V;
import com.diboot.core.util.S;
import com.diboot.core.util.ContextHolder;
import com.diboot.core.vo.JsonResult;
import com.diboot.core.vo.LabelValue;
import com.diboot.core.vo.Pagination;
import com.diboot.core.controller.BaseCrudRestController;
import com.diboot.iam.annotation.OperationCons;
import com.diboot.iam.annotation.BindPermission;
import com.diboot.iam.annotation.Log;
import com.hzncc.flowable_diboot.entity.ProductData;
import com.hzncc.flowable_diboot.entity.SpeechcraftData;
import com.hzncc.flowable_diboot.mapper.ProductDataMapper;
import com.hzncc.flowable_diboot.mapper.ProductSortMapper;
import com.hzncc.flowable_diboot.mapper.SpeechcraftDataMapper;
import com.hzncc.flowable_diboot.vo.ProductSortMenuVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hzncc.flowable_diboot.entity.ProductSort;
import com.hzncc.flowable_diboot.service.ProductSortService;
import com.hzncc.flowable_diboot.vo.ProductSortDetailVO;
import com.hzncc.flowable_diboot.vo.ProductSortListVO;
import com.hzncc.flowable_diboot.dto.ProductSortDTO;
import java.util.List;

/**
* 产品分类 相关rest接口
* @author MyName
* @version 1.0
* @date 2024-07-04
* Copyright © MyCorp
*/
@Slf4j
@RequestMapping("/product-sort")
@BindPermission(name = "产品分类")
@RestController
public class ProductSortController extends BaseCrudRestController<ProductSort> {

    @Autowired
    private ProductSortService productSortService;
    @Autowired
    private ProductSortMapper productSortMapper;
    @Autowired
    private ProductDataMapper productDataMapper;
    @Autowired
    private SpeechcraftDataMapper speechcraftDataMapper;

    /**
    * 查询资源对象的列表VO记录
    * <p>
    * url请求参数示例: ?fieldA=abc&pageSize=20&pageIndex=1&orderBy=id
    * </p>
    * @return
    * @throws Exception
    */
    @Log(operation = OperationCons.LABEL_LIST)
    @BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
    @GetMapping()
    public JsonResult<List<ProductSortListVO>> getListVOMapping(ProductSortDTO queryDto, Pagination pagination) throws Exception {
        return super.getViewObjectList(queryDto, pagination, ProductSortListVO.class);
    }

    /**
    * 根据id查询资源对象的详情VO
    * @param id ID
    * @return
    * @throws Exception
    */
    @Log(operation = OperationCons.LABEL_DETAIL)
    @BindPermission(name = OperationCons.LABEL_DETAIL, code = OperationCons.CODE_READ)
    @GetMapping("/{id}")
    public JsonResult<ProductSortDetailVO> getDetailVOMapping(@PathVariable("id")String id) throws Exception {
        ProductSortDetailVO detailVO = productSortService.getViewObject(id, ProductSortDetailVO.class);
        return JsonResult.OK(detailVO);
    }


    /**
    * 创建资源对象数据
    * @param productSort
    * @return JsonResult
    * @throws Exception
    */
    @Log(operation = OperationCons.LABEL_CREATE)
    @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @PostMapping()
    public JsonResult<?> createModelMapping(@RequestBody ProductSort productSort) throws Exception {

        boolean success = productSortService.createEntity(productSort);
        log.debug("新建数据 产品分类:{} {}", productSort.getId(), success? "成功":"失败");
        return JsonResult.OK(productSort.getId());
    }

    /**
    * 批量创建资源对象数据
    * @param productSortList
    * @return JsonResult
    * @throws Exception
    */
    @Log(operation = OperationCons.LABEL_BATCH_UPDATE)
    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PostMapping("/batch")
    public JsonResult<?> batchCreateModelMapping(@RequestBody List<ProductSort> productSortList) throws Exception {
        boolean success = productSortService.createEntities(productSortList);
        log.debug("批量新建数据 产品分类 {}", success? "成功":"失败");
        return JsonResult.OK(success);
    }

    /**
    * 根据id更新资源对象
    * @param productSort
    * @return JsonResult
    * @throws Exception
    */
    @Log(operation = OperationCons.LABEL_UPDATE)
    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PutMapping("/{id}")
    public JsonResult<?> updateModelMapping(@PathVariable("id")String id, @RequestBody ProductSort productSort) throws Exception {
//        ProductSort entity = productSortService.getEntity(id);
//        if ( !productSort.getPid().equals(entity.getPid()) ){
//            if( productSort.getCategory().equals("产品") ){
//                productDataMapper.updatePid(entity.getPid(),productSort.getCategory());
//            }else if (productSort.getCategory().equals("话术")){
//                QueryWrapper<SpeechcraftData>  scqw = new QueryWrapper<>();
//                scqw.eq("category",id);
//                List<SpeechcraftData> speechcraftData = speechcraftDataMapper.selectList(scqw);
//
//            }
//            boolean success = productSortService.updateEntity(productSort);
//            log.debug("更新数据 产品分类:{} {}", id, success? "成功":"失败");
//            return JsonResult.OK(success);
//        }else {
            boolean success = productSortService.updateEntity(productSort);
            log.debug("更新数据 产品分类:{} {}", id, success? "成功":"失败");
            return JsonResult.OK(success);
//        }

    }

    /**
    * 根据id删除资源对象
    * @param id
    * @return
    * @throws Exception
    */
    @Log(operation = OperationCons.LABEL_DELETE)
    @BindPermission(name = OperationCons.LABEL_DELETE, code = OperationCons.CODE_WRITE)
    @GetMapping("/delect")
    public JsonResult<?> deleteModelMapping(String id,String category) throws Exception {
        QueryWrapper<ProductSort> sqw = new QueryWrapper<>();
        sqw.eq("pid",id);
        List<ProductSort> sortList = productSortMapper.selectList(sqw);
        if ( sortList != null && sortList.size() != 0){
            return JsonResult.FAIL_EXCEPTION("存在子目录");
        }
        if( category.equals("产品") ){
            QueryWrapper<ProductData>  pdqw = new QueryWrapper<>();
            pdqw.eq("category",id);
            List<ProductData> productDataList = productDataMapper.selectList(pdqw);
            if ( productDataList != null && productDataList.size() != 0){
                return JsonResult.FAIL_EXCEPTION("当前目录下存在数据");
            }
        }else if (category.equals("话术")){
            QueryWrapper<SpeechcraftData>  scqw = new QueryWrapper<>();
            scqw.eq("category",id);
            List<SpeechcraftData> speechcraftData = speechcraftDataMapper.selectList(scqw);
            if ( speechcraftData != null && speechcraftData.size() != 0){
                return JsonResult.FAIL_EXCEPTION("当前目录下存在数据");
            }
        }


        boolean success = productSortService.deleteEntity(id);
        log.debug("删除数据 产品分类:{} {}", id, success? "成功":"失败");
        return JsonResult.OK(success);
    }

    /**
    * 根据ids集合删除资源对象
    * @param ids
    * @return
    * @throws Exception
    */
    @Log(operation = OperationCons.LABEL_BATCH_DELETE)
    @BindPermission(name = OperationCons.LABEL_DELETE, code = OperationCons.CODE_WRITE)
    @PostMapping("/batch-delete")
    public JsonResult<?> deleteEntityMapping(@RequestBody List<String> ids) throws Exception {
        boolean success = productSortService.deleteEntities(ids);
        log.debug("批量删除数据 产品分类:{} {}", ids, success? "成功":"失败");
        return JsonResult.OK(success);
    }

    /**
     * 根据id查询资源对象的详情VO
     */
    @Log(operation = OperationCons.LABEL_DETAIL)
    @BindPermission(name = OperationCons.LABEL_DETAIL, code = OperationCons.CODE_READ)
    @GetMapping("/getMeneTree")
    public JsonResult getMene(String category) throws Exception {
        List<ProductSortMenuVO> tree = productSortService.getTree(category);
        return JsonResult.OK(tree);
    }
}